package com.xy.yunduo.utils.http;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WebUtil {
    private static final String DEFAULT_CONTENT_TYPE = "text/plain";
    private static final Map<String, String> mimeTypes = new HashMap();
    private static final Pattern VERSIONED_FILE_PATTERN;

    private WebUtil() {
    }

    public static String getContentType(String fileName) {
        int idx = fileName.lastIndexOf(".");
        String suffix = "";
        if (idx != -1) {
            suffix = fileName.substring(idx + 1);
        }

        String contentType = (String)mimeTypes.get(suffix);
        if (contentType == null) {
            contentType = "text/plain";
        }

        return contentType + "; charset=UTF-8";
    }

    public static String normalizeVersionedFileName(String name) {
        Matcher m = VERSIONED_FILE_PATTERN.matcher(name);
        if (m.find()) {
            name = m.group(1) + "." + m.group(3);
            return name;
        } else {
            return name;
        }
    }

    public static String getRealIP(HttpServletRequest request) {
        String ip = request.getHeader("HTTP_X_REAL_IP");
        if (ip == null) {
            ip = request.getHeader("X-Real-Ip");
        }

        if (ip == null) {
            ip = request.getHeader("X-Forwarded-For");
        }

        if (ip == null) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }

    public static boolean isAjax(HttpServletRequest request) {
        String requestType = request.getHeader("X-Requested-With");
        return "XMLHttpRequest".equals(requestType);
    }

    public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie c = getCookie(request, name);
        return c == null ? null : c.getValue();
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        } else {
            Cookie[] var3 = cookies;
            int var4 = cookies.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                Cookie c = var3[var5];
                if (name.equals(c.getName())) {
                    return c;
                }
            }

            return null;
        }
    }

    public static void addPermanencyCookie(HttpServletResponse response, String name, String value) {
        addPermanencyCookie(response, name, value, "/");
    }

    public static void addPermanencyCookie(HttpServletResponse response, String name, String value, String path) {
        int expiry = 315360000;
        addCookie(response, name, value, path, expiry, true);
    }

    public static void addCookie(HttpServletResponse response, String name, String value, String path, int expiry, boolean httpOnly) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(expiry);
        cookie.setPath(path);
        //cookie.setHttpOnly(httpOnly);
        response.addCookie(cookie);
    }

    static {
        mimeTypes.put("txt", "text/plain");
        mimeTypes.put("html", "text/html");
        mimeTypes.put("css", "text/css");
        mimeTypes.put("js", "application/x-javascript");
        mimeTypes.put("json", "application/json");
        mimeTypes.put("gif", "image/gif");
        mimeTypes.put("png", "image/png");
        mimeTypes.put("jpg", "image/jpeg");
        mimeTypes.put("jpeg", "image/jpeg");
        mimeTypes.put("xml", "text/xml");
        mimeTypes.put("ico", "image/x-icon");
        mimeTypes.put("svg", "image/svg+xml");
        mimeTypes.put("doc", "application/msword");
        mimeTypes.put("pdf", "application/pdf");
        mimeTypes.put("mp4", "video/mp4");
        mimeTypes.put("flv", "video/x-flv");
        mimeTypes.put("avi", "video/x-msvideo");
        VERSIONED_FILE_PATTERN = Pattern.compile("^(.*)-([a-zA-Z0-9]{40})\\.(\\w+)$");
    }
}
